import Qs from 'qs';
import axios from "axios";
import {message} from "ant-design-vue";

axios.defaults.headers["X-Requested-With"] = "XMLHttpRequest";
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

const BASE_URL = "/nav";

const http = (url, params, method = 'get') => {
    let data = method === 'get' ? {params} : {data: Qs.stringify(params)};

    return new Promise((resolve, reject) => {
        // 发起一个POST请求
        axios({
            url: `${BASE_URL}${url}`,
            method,
            ...data,
        })
            .then((res) => {
                if (res.status === 200) {
                    resolve(res.data);
                } else {
                    message.error(res.msg);
                }
            })
            .catch((res) => {
                reject(res);
            });
    });
};

export default http;
